using UnityEngine;

public class Bullet : MonoBehaviour
{
    public float speed = 10f; // 子弹的速度
    public float lifetime = 2f; // 子弹的生命周期
    public GameObject impactEffect; //子弹击中特效
    public string targetTag = "Enemy"; // 子弹的目标标签

    private void Update()
    {
        // 每帧更新子弹的位置
        transform.Translate(Vector3.forward * speed * Time.deltaTime);

        lifetime -= Time.deltaTime; // 更新计时器
        if (lifetime <= 0) Destroy(gameObject); // 时间小于0，销毁子弹
    }

    private void OnTriggerEnter(Collider other)
    {

        if (other.gameObject.tag == targetTag)
        {
            other.gameObject.SendMessage("hurt", 1);
        }
        if (other.gameObject.tag == "Wall" || other.gameObject.tag == targetTag)
        {
            if (impactEffect != null)// 创建特效
            {
                Instantiate(impactEffect, transform.position, transform.rotation);
            }
            Destroy(gameObject);
        }

    }
}